package com.ibuu.demos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ibuu.demos.mapper.*;
import com.ibuu.demos.pojo.Categories;
import com.ibuu.demos.pojo.Details;
import com.ibuu.demos.pojo.Num;
import com.ibuu.demos.pojo.ShopingCar;
import com.ibuu.demos.service.CategoriesService;
import com.ibuu.dto.OrderDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories> implements CategoriesService {
Integer a=5;
    @Autowired
    private CategoriesMapper categoriesMapper;

    @Autowired
    private ShopingCarMapper shopingCarMapper;
    @Autowired
    NewsMapper newsMapper;

    @Autowired
    private PayMapper PayMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Categories> getCategories() {
        LambdaQueryWrapper<Categories> qw = new LambdaQueryWrapper<>();
        qw.eq(Categories::getStatus, 1);
        return categoriesMapper.selectList(qw);
    }

    @Override
    public List<Categories> getCategoriesBykind(Integer kind) {
        LambdaQueryWrapper<Categories> qw = new LambdaQueryWrapper<>();
        qw.eq(Categories::getKind, kind);
        List<Categories> s = categoriesMapper.selectList(qw);
        return s;
    }


    //购物车添加
    @Override
    public void addCategories(Categories categories, HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        ShopingCar shopingCarById = getShopingCarById(categories.getShopid(), username);

        if (shopingCarById != null) {
            shopingCarById.setNum(shopingCarById.getNum() + 1);
            shopingCarMapper.updateById(shopingCarById);
        } else {
            ShopingCar shopingCar = new ShopingCar();
            BeanUtils.copyProperties(categories, shopingCar);
            shopingCar.setUser(username);
            shopingCar.setNum(1);
            shopingCar.setShopid(categories.getShopid());
            String s = categories.getShopid().toString() + username;
            shopingCar.setId(s);
            shopingCarMapper.insert(shopingCar);
        }
    }

    //购物车查询
    @Override
    public List<ShopingCar> getShopingCar(String name) {
        // 创建查询条件
        LambdaQueryWrapper<ShopingCar> qw = new LambdaQueryWrapper<>();
        qw.eq(ShopingCar::getUser, name);
        // 执行查询
        List<ShopingCar> shopingCars = shopingCarMapper.selectList(qw);
        // 遍历购物车列表，删除数量为0的项
        for (ShopingCar shopingCar : shopingCars) {
            if (shopingCar.getNum() == 0) {
                shopingCarMapper.deleteById(shopingCar.getShopid());
            }
        }
        // 返回购物车列表
        return shopingCars;
    }

    //购物车按id查询
    @Override
    public ShopingCar getShopingCarById(Integer id, String name) {
        LambdaQueryWrapper<ShopingCar> qw = new LambdaQueryWrapper<>();

        qw.eq(ShopingCar::getUser, name);
        qw.eq(ShopingCar::getShopid, id);
        ShopingCar shopingCar = shopingCarMapper.selectOne(qw);

        return shopingCar;
    }


    @Override
    public void jianCategories(Categories categories, HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        ShopingCar shopingCarById = getShopingCarById(categories.getShopid(), username);

        if (shopingCarById != null) {
            Integer num = shopingCarById.getNum();
            if (num > 1) {
                shopingCarById.setNum(num - 1);
                shopingCarMapper.updateById(shopingCarById);
            } else {
                shopingCarMapper.deleteById(shopingCarById.getId());
            }
        }
    }

    //清空购物车
    @Override
    public void deleteCategories(String username) {
        LambdaQueryWrapper<ShopingCar> qw = new LambdaQueryWrapper<>();
        qw.eq(ShopingCar::getUser, username);
        shopingCarMapper.delete(qw);
    }


    //查询订单
//    @Override
//    public List<OrderDTO> getDetails(HttpServletRequest request) {
//        String username = (String) request.getSession().getAttribute("username");
//        LambdaQueryWrapper<Details> qw = new LambdaQueryWrapper<>();
//        qw.eq(Details::getUsername, username);
//        List<Details> details = PayMapper.selectList(qw);
//
//        ArrayList<OrderDTO> orderDTOS = new ArrayList<>();
//
//        for (Details detail : details) {
//            OrderDTO orderDTO = new OrderDTO();
//            orderDTO.setDetails(details);
//            BeanUtils.copyProperties(detail, orderDTO);
//            orderDTOS.add(orderDTO);
//            //计算价格
//            Integer fprice = 0;
//            Integer price = detail.getPrice();
//            Integer num = detail.getNum();
//            fprice += price * num;
//            orderDTO.setFprice(fprice);
//        }
//
//        return orderDTOS;
//    }
    @Override
    public Page<OrderDTO> getDetails(HttpServletRequest request, int pageNum, int pageSize) {
        // 获取用户名
        String username = (String) request.getSession().getAttribute("username");
// 创建查询条件
        LambdaQueryWrapper<Details> qw = new LambdaQueryWrapper<>();
        qw.eq(Details::getUsername, username);
// 分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Details> details = PayMapper.selectList(qw);
// 创建订单映射
        Map<String, OrderDTO> orderMap = new HashMap<>();

        for (Details detail : details) {
            String orderId = detail.getId();
            OrderDTO orderDTO = orderMap.getOrDefault(orderId, new OrderDTO());
            orderDTO.setId(orderId);
            orderDTO.setUsername(detail.getUsername());
            Integer a = orderDTO.getFprice();
            int b = detail.getPrice() * detail.getNum();
            int i = a == null ? b : a + b;
            orderDTO.setFprice(i);
            if (orderDTO.getDetails() == null) {
                orderDTO.setDetails(new ArrayList<>());
            }
            orderDTO.getDetails().add(detail);
            orderMap.put(orderId, orderDTO);
        }

// 转换为订单列表
        List<OrderDTO> orderDTOS = new ArrayList<>(orderMap.values());
// 创建分页信息
        PageInfo<OrderDTO> pageInfo = new PageInfo<>(orderDTOS);
// 创建分页对象
        Page<OrderDTO> page = new Page<>(pageNum, pageSize);
        page.setRecords(pageInfo.getList());
        page.setTotal(pageInfo.getTotal());
// 返回分页结果
        return page;
    }

    @Override
    public void add(Categories categories) {
//categories.setShopid(a++);
categories.setStatus(1);
categories.setImage("/image/111.png");
categories.setNum(0);
        LambdaQueryWrapper<Categories> qw = new LambdaQueryWrapper<>();
        //取id最大值
        qw.orderByDesc(Categories::getShopid);
qw.last("limit 1");
        Categories categories1 = categoriesMapper.selectOne(qw);
        Integer shopid = categories1.getShopid();
        categories.setShopid(shopid+1);


        categoriesMapper.insert(categories);
    }

    @Override
    public void removeid(Integer id) {
        LambdaQueryWrapper<Categories> qw = new LambdaQueryWrapper<>();
        qw.eq(Categories::getShopid, id);
        categoriesMapper.delete(qw);
        System.out.println("删除成功");
    }

    @Override
    public void updateid(Categories categories) {
        LambdaQueryWrapper<Categories> qw = new LambdaQueryWrapper<>();
        Integer shopid = categories.getShopid();
        qw.eq(Categories::getShopid, shopid);
        categoriesMapper.update(categories,qw);
        System.out.println("修改成功");
    }

    @Override
    public Page<OrderDTO> admingetDetails(int pageNum, int pageSize) {
// 创建查询条件
        LambdaQueryWrapper<Details> qw = new LambdaQueryWrapper<>();
// 分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Details> details = PayMapper.selectList(qw);
// 创建订单映射
        Map<String, OrderDTO> orderMap = new HashMap<>();

        for (Details detail : details) {
            String orderId = detail.getId();
            OrderDTO orderDTO = orderMap.getOrDefault(orderId, new OrderDTO());
            orderDTO.setId(orderId);
            orderDTO.setUsername(detail.getUsername());
            Integer a = orderDTO.getFprice();
            int b = detail.getPrice() * detail.getNum();
            int i = a == null ? b : a + b;
            orderDTO.setFprice(i);
            if (orderDTO.getDetails() == null) {
                orderDTO.setDetails(new ArrayList<>());
            }
            orderDTO.getDetails().add(detail);
            orderMap.put(orderId, orderDTO);
        }

// 转换为订单列表
        List<OrderDTO> orderDTOS = new ArrayList<>(orderMap.values());
// 创建分页信息
        PageInfo<OrderDTO> pageInfo = new PageInfo<>(orderDTOS);
// 创建分页对象
        Page<OrderDTO> page = new Page<>(pageNum, pageSize);
        page.setRecords(pageInfo.getList());
        page.setTotal(pageInfo.getTotal());
// 返回分页结果
        return page;

    }
}



